
$(function(){
	$("select").each(function(){
		$(this).append("<option value=''>---请选择---</option>");
	});

	var basePath = $("#basePath").val();
	$.ajax({
		url: basePath + "base_info/ajax_un_pigeonhole_base_data",
		type: "GET",
		dataType: "json",
		success: function( datas ) {
			console.log(datas);
			init(datas);
			
		}
	});
});

function init(baseInfo){
	var medicalDepts = baseInfo['medicalDepts'];
	$("select[name='outHospitalDeptCode'],[name='outDeptCode']").each(function(){
		var options = "";
		for(var index in medicalDepts){
			var medicalDept = medicalDepts[index];
	    	options+="<option value='"+medicalDept.uniq_code+"'>"+medicalDept.label+"</option>"
	    } 
       $(this).append(options);
       
	});
}


/*点击查询按钮*/
function queryBtnClick(){
	$("#queryForm").removeAttr("action",null);
	queryFormSubmit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	layer.load(1,{offset: '300px'});
	clearPage();
	$.ajax({
		url: basePath + "medical_record/ajax_quality_control_score_rate",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				
				var qualityControlScoreRates = data['data'];
				
				if(qualityControlScoreRates.length > 0){
					$("#quality_control_rate").html("<input type='button' value='导出' onclick='exportToExcel()'/>");
					
					layer.msg("查询成功",{offset: '300px'});
					
					addRows(qualityControlScoreRates);
				}else{
					layer.msg("查询结果为空",{offset: '300px'});
				}
				
			}else{
				layer.msg("查询失败",{offset: '300px'});
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			layer.msg("未知错误，请联系管理员",{offset: '300px'});
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			parent.document.getElementById("mainContent").height=document.body.scrollHeight;
		}
	});
	
};

/*添加列表行*/
function addRows(qualityControlScoreRates){
	var basePath = $("#basePath").val();
	var outHospitalStartDate = $("input[name='outHospitalStartDate']").val();
	var outHospitalEndDate = $("input[name='outHospitalEndDate']").val();
	for(var index in qualityControlScoreRates){
		var qualityControlScoreRate = qualityControlScoreRates[index];
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden").removeAttr("id");
		add_content.find("span[class='out_dept_name']").html(qualityControlScoreRate.out_dept_name);
		/*if(qualityControlScoreRate.total_count > 0){
			var totalContLabel = "<a href=''>"+qualityControlScoreRate.total_count+"</a>"
			add_content.find("span[class='total_count']").html(totalContLabel);
		}else{
			add_content.find("span[class='total_count']").html(qualityControlScoreRate.total_count);
		}*/
		
		add_content.find("span[class='total_count']").html(qualityControlScoreRate.total_count);
		add_content.find("span[class='quality_control_count']").html(qualityControlScoreRate.quality_control_count);
		add_content.find("span[class='quality_control_rate']").html(number_percent(qualityControlScoreRate.quality_control_rate)+"%");
		if(qualityControlScoreRate.quality_control_a_count > 0){
			var params = "?outHospitalStartDate="+outHospitalStartDate+"&outHospitalEndDate="+outHospitalEndDate+
			"&outDeptCode="+qualityControlScoreRate.out_dept_code+"&minScore=89.5&maxScore=100"
			var totalContALabel = "<a target='_blank' href='"+basePath+"medical_record_quality_control/office_control_record"+params+"'>"+qualityControlScoreRate.quality_control_a_count+"</a>"
			add_content.find("span[class='quality_control_a_count']").html(totalContALabel);
		}else{
			add_content.find("span[class='quality_control_a_count']").html(qualityControlScoreRate.quality_control_a_count);
		}
		/*add_content.find("span[class='quality_control_a_count']").html(qualityControlScoreRate.quality_control_a_count);*/
		add_content.find("span[class='quality_control_a_rate']").html(number_percent(qualityControlScoreRate.quality_control_a_rate)+"%");
		if(qualityControlScoreRate.quality_control_b_count > 0){
			var params = "?outHospitalStartDate="+outHospitalStartDate+"&outHospitalEndDate="+outHospitalEndDate+
			"&outDeptCode="+qualityControlScoreRate.out_dept_code+"&minScore=74.5&maxScore=89.5"
			var totalContBLabel = "<a target='_blank' href='"+basePath+"medical_record_quality_control/office_control_record"+params+"'>"+qualityControlScoreRate.quality_control_b_count+"</a>"
			add_content.find("span[class='quality_control_b_count']").html(totalContBLabel);
		}else{
			add_content.find("span[class='quality_control_b_count']").html(qualityControlScoreRate.quality_control_b_count);
		}
//		add_content.find("span[class='quality_control_b_count']").html(qualityControlScoreRate.quality_control_b_count);
//		
		add_content.find("span[class='quality_control_b_rate']").html(number_percent(qualityControlScoreRate.quality_control_b_rate)+"%");
		
		if(qualityControlScoreRate.quality_control_c_count > 0){
			var params = "?outHospitalStartDate="+outHospitalStartDate+"&outHospitalEndDate="+outHospitalEndDate+
			"&outDeptCode="+qualityControlScoreRate.out_dept_code+"&minScore=0&maxScore=74.5"
			var totalContCLabel = "<a target='_blank' href='"+basePath+"medical_record_quality_control/office_control_record"+params+"'>"+qualityControlScoreRate.quality_control_c_count+"</a>"
			add_content.find("span[class='quality_control_c_count']").html(totalContCLabel);
		}else{
			add_content.find("span[class='quality_control_c_count']").html(qualityControlScoreRate.quality_control_c_count);
		}
		
		/*add_content.find("span[class='quality_control_c_count']").html(qualityControlScoreRate.quality_control_c_count);*/
		add_content.find("span[class='quality_control_c_rate']").html(number_percent(qualityControlScoreRate.quality_control_c_rate)+"%");
		$("#query_show_table tbody").append(add_content);
    } 
	
}

/*清空列表*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
	$("#quality_control_rate").html("");
}


function number_percent(value) {
	if (value != null) {
		return (value).toFixed(2);
	} else {
		return;
	}
}

function exportToExcel(){
	var basePath = $("#basePath").val();
	$("#queryForm").attr("action",basePath+"excel/quality_control_score_rate");
	$("#queryForm").submit();
}

